package com.xtm.exercise.utils;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.lionsoul.ip2region.xdb.Searcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 获取ip位置
 */
public class Ip2RegionUtils {
   private static Logger logger = LoggerFactory.getLogger(Ip2RegionUtils.class);

   public static void main(String[] args) {
      String clientIp = "";
      Searcher ip2RegionSearch = Ip2RegionUtils.getIp2RegionSearch();
      String ip2Region = Ip2RegionUtils.getIp2RegionByIp(clientIp, ip2RegionSearch);
      if (StringUtils.isNotBlank(ip2Region)) {
         String[] ip2RegionSplit = ip2Region.split("\\|");
         if (Objects.nonNull(ip2RegionSplit) && ip2RegionSplit.length > 0) {
            String ip2Reqion = ip2RegionSplit[2];
         }
      }
   }

   public static String getIp2RegionByIp(String ipAddess, Searcher ip2RegionSearch) {
      try {
         if (StringUtils.isBlank(ipAddess)) {
            return "";
         } else {
            if (Objects.isNull(ip2RegionSearch)) {
               ip2RegionSearch = getIp2RegionSearch();
            }

            long sTime = System.nanoTime();
            String region = ip2RegionSearch.search(ipAddess);
            long cost = TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - sTime);
            logger.info("位置是#{}, 查询次数#{}, 耗时#{}μs", new Object[]{region, ip2RegionSearch.getIOCount(), cost});
            return region;
         }
      } catch (Exception var7) {
         logger.error("根据IP查询位置失败#{}", var7);
         return "";
      }
   }

   public static Searcher getIp2RegionSearch() {
      String dbPath = "";

      try {
         String os = System.getProperty("os.name");
         if (os.toLowerCase().startsWith("win")) {
            File file = ResourceUtils.getFile("classpath:ip2region.xdb");
            dbPath = file.getAbsolutePath();
            logger.info("os.name#win#{}", dbPath);
         } else {
            Resource resource = (new DefaultResourceLoader()).getResource("classpath:ip2region.xdb");
            logger.info("os.name#linux#resource#{}", resource.exists());
            String tempPath = System.getProperty("user.dir") + "/ip2region";
            logger.info("os.name#linux#tempPath#{}", tempPath);
            String tempFile = tempPath + "/ip2region.xdb";
            File file = new File(tempPath);
            if (!file.exists()) {
               logger.info("os.name#linux#创建文件夹#");
               file.mkdir();
            }

            IOUtils.copy(resource.getInputStream(), new FileOutputStream(new File(tempFile)));
            dbPath = tempPath + "/ip2region.xdb";
         }

         logger.info("获取到文件路径是#{}", dbPath);
         byte[] cBuff = Searcher.loadContentFromFile(dbPath);
         Searcher searcher = Searcher.newWithBuffer(cBuff);
         return searcher;
      } catch (Exception var7) {
         logger.error("加载ip2region.xdb数据失败#{}", var7);
         return null;
      }
   }
}
